package com.zx;

public class SimpleBeanConvertor {
    private static Logger logger = Logger.getLogger(SimpleBeanConvertor.class.getName());

    public static <T> T convert(HttpServletRequest request,Class<T> targetType) throws IllegalAccessException, InstantiationException, ParseException, InvocationTargetException {
        Map<String,String[]> params = request.getParameterMap();
        if (params.size() == 0) return null;
        T target = targetType.newInstance();
        // 获取所有参数请求
        Method[] allMethods = targetType.getMethods();
        if (allMethods != null && allMethods.length > 0){
            for (Method allMethod : allMethods) {
                if (allMethod.getName().startsWith("set")){
                    Class[] args = allMethod.getParameterTypes();
                    if (args.length == 1){
                        String paramName = allMethod.getName().substring(3,4).toLowerCase() + allMethod.getName().substring(4);
                        if (params.containsKey(paramName)){
                            Object value = null;
                            try {
                                value = parseValue(params.get(paramName),args[0]);
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            } catch (NoSuchMethodException e) {
                                e.printStackTrace();
                            }
                            allMethod.invoke(target,value);
                        }else if (Boolean.class == args[0] || boolean.class == args[0]){
                            allMethod.invoke(target,false);
                        }
                    }
                }
            }
        }
        return target;
    }

    private static Object parseValue(String[] value, Class type) throws ParseException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        // 简答
        if (value == null)
            return null;
        if (value instanceof String[])
            return value;

        if (String.class == type)
            return value[0];
        if (java.util.Date.class == type)
            return new SimpleDateFormat("yyyy-MM-dd").parse(value[0]);
        if (boolean.class == type || Boolean.class == type)
            return true;
        if (char.class == type || Character.class == type)
            return  value[0].charAt(0);
        if (int.class == type || Integer.class == type)
            return Integer.valueOf(value[0]);
        if (short.class == type || byte.class == type || long.class == type || float.class == type || double.class == type)
            type = Class.forName("java.lang." + type.getName().substring(0,1).toUpperCase() + type.getName().substring(1));
        if (type.getName().startsWith("java.lang.") || type.getName().startsWith("java.math."))
            return type.getConstructor(String.class).newInstance(value[0]);
        return false;
    }
}
